<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-05 11:29
 *
 * Version: 2.0
 */

namespace App\Modules\DataCenter\Controllers;


use App\Lib\Facades\FLAuth;
use App\Lib\Logs\Logs;
use App\Lib\Utilities;
use App\Modules\DataCenter\Services\StatisticsService;
use App\Modules\DataCenter\Transformers\getContributionListTransformer;
use App\Modules\DataCenter\Transformers\getStoreHeaderInfoTransformer;
use App\Modules\DataCenter\Validators\StatisticsValidator;
use App\Modules\Employee\Services\EmployeeService;
use App\Modules\MemberCard\Services\MemberCardService;
use App\Modules\Order\Services\OrderService;
use App\Modules\Store\Services\StoreService;
use Flower\Core\Http\BaseController;
use Flower\Core\Http\Request;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Validation\ValidationException;

class StatisticsController extends BaseController
{
	private $statisticsService;
	private $memberCardService;
	private $orderService;
	private $employeeService;
	private $storeService;

	public function __construct(StatisticsService $statisticsService, MemberCardService $memberCardService, OrderService $orderService, EmployeeService $employeeService, StoreService $storeService
	)
	{
		$this->statisticsService = $statisticsService;
		$this->memberCardService = $memberCardService;
		$this->orderService = $orderService;
		$this->employeeService = $employeeService;
		$this->storeService = $storeService;

	}

	public function getRules()
	{
		return StatisticsValidator::validator();
	}

	/**
	 * 计算店铺排名 跑定时任务
	 * @return mixed
	 */
	public function rankStore()
	{
		$res = $this->storeService->rankStore();
		if (!$res) return [
			'status' => !$res,
			'msg'    => '店铺排名写入失败！'
		];
		return [
			'status' => $res,
			'msg'    => '店铺排名写入成功！'
		];
	}


	/**
	 * 店长数据中心头部信息
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function getDataStoreHead(Request $request)
	{
		$info = $this->statisticsService->getStoreBossHeaderInfo($request->store_id, FLAuth::id());
		return $this->display((new getStoreHeaderInfoTransformer($info))->transformer(), "数据中心头部信息获取成功");
	}








	/**
	 * 数据中心头部信息
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getBossDataCenter(Request $request)
	{
		$head = $this->statisticsService->getStoreBossHeaderInfo($request->store_id, FLAuth::id());
		$list['head'] = (new getStoreHeaderInfoTransformer($head))->transformer();
		$list['buss']['items'] = $this->statisticsService->getOperatingData(trim($request->store_id));
		$list['visitors'] = $this->statisticsService->getVisitorData(trim($request->store_id), trim($request->date_type));
//		$list['orders'] = $this->statisticsService->getOrderData(trim($request->store_id), trim($request->date_type));
		$list['order_pie'] = $this->statisticsService->getPieChartOrderData(trim($request->store_id), trim($request->date_type));
//		$list['payment_bar']['payments'] = $this->statisticsService->getPaymentBarChartList(trim($request->store_id), trim($request->date_type));
		$list['member_card'] = $this->statisticsService->getMemberCardDataCenter(trim($request->store_id), trim($request->date_type));
//		$list['member_card']
//		foreach ($memberCardList['flow_list'] as $v){
//			$v
//		}
//		$list['appoint']['items'] = $this->statisticsService->getOrderPointData(trim($request->store_id), trim($request->date_type));
		$list['barber_rank']['items'] = $this->statisticsService->getBarberRankData(trim($request->store_id), 1, trim($request->date_type));
//		$list['service_rank']['items'] = $this->statisticsService->getOrderServiceData(trim($request->store_id), trim($request->date_type), 1, 1);
		$list['contributions']['items'] = $this->statisticsService->getContributionAmount(trim($request->store_id), trim($request->date_type), 1, 1);
		$list['wages']['items'] = $this->statisticsService->getCenterUserSalaryData(trim($request->store_id));
		$list['analysis']['items'] = $this->statisticsService->getStoreBusinessData(trim($request->date_type), $request->store_id);
		$list['profit'] =  $this->statisticsService->getStoreProfit(trim($request->date_type), $request->store_id);
		$list['check_payment']['items'] = $this->statisticsService->getStorePayment(trim($request->date_type), $request->store_id);
		$list['achievement']['items'] = $data = $this->statisticsService->getStoreAchievement(trim($request->date_type), $request->store_id);
		return $this->display($list);
	}

	/**
	 * 店长数据中心昨日经营数据// 今日经营数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 21:35
	 */
	public function getOperatingDataYesterday(Request $request)
	{
		$list = $this->statisticsService->getOperatingData(trim($request->store_id));
		return $this->display($list);
	}


	/**
	 * 店长数据中心 访客数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 10:10
	 */
	public function getVisitorData(Request $request)
	{
		$list = $this->statisticsService->getVisitorData(trim($request->store_id), trim($request->date_type));
		return $this->display($list);
	}

	/**
	 * 店长数据中心 访客数据列表
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 10:10
	 */
	public function getVisitorList(Request $request)
	{
		$list = $this->statisticsService->getVisitorList(trim($request->store_id), trim($request->date_type), trim($request->page_size));
		return $this->display($list);
	}

	/**
	 * 店长数据中心 订单数据
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 15:16
	 */
	public function getOrderData(Request $request)
	{
		$list = $this->statisticsService->getOrderData(trim($request->store_id), trim($request->date_type));
		return $this->display($list);
	}


	/**
	 * 店长数据中心 订单数据列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 15:07
	 */
	public function getOrderList(Request $request)
	{
		$list = $this->statisticsService->getOrderList(trim($request->store_id), trim($request->page_size), trim($request->date_type));
		return $this->display(Utilities::dealPage($list));
	}

	/**
	 * 店长数据中心 订单饼图数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 16:18
	 */
	public function getPieChartOrderData(Request $request)
	{
		$list = $this->statisticsService->getPieChartOrderData(trim($request->store_id), trim($request->date_type));
		return $this->display($list);
	}


	/**
	 * 店长数据中心 支付方式柱状图
	 * @param Request $request
	 * @return mixed
	 */
	public function getPaymentBarChartList(Request $request)
	{
		$list = $this->statisticsService->getPaymentBarChartList(trim($request->store_id), trim($request->date_type));
		return $this->display($list);
	}


	/**
	 * 店长数据中心 会员卡数据
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getMemberCardDataCenter(Request $request)
	{
		$list = $this->statisticsService->getMemberCardDataCenter(trim($request->store_id), trim($request->date_type));

		return $this->display($list);
	}


	/**
	 * 会员卡订单列表数据
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-19 20:58
	 */
	public function getCardFlowDataCenter(Request $request)
	{
		$list = $this->statisticsService->getCardFlowList(trim($request->store_id), trim($request->page_size), trim($request->date_type));
		return $this->display(Utilities::dealPage($list));
	}


	/**
	 * 指定用户占比数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-20 11:22
	 */
	public function getOrderPointData(Request $request)
	{
		$list = $this->statisticsService->getOrderPointData(trim($request->store_id), trim($request->date_type));
		return $this->display($list);
	}


	/**
	 * 指定用户占订单列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-20 14:21
	 */
	public function getOrderPointListData(Request $request)
	{
		$list = $this->statisticsService->getOrderPointList(trim($request->store_id), trim($request->user_id), trim($request->date_type), trim($request->page_size));
		$list = Utilities::dealPage($list);
		return $this->display($list);
	}


	/**
	 * 理发师排名数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-20 15:48
	 */
	public function getBarberRankData(Request $request)
	{
		$list = $this->statisticsService->getBarberRankData(trim($request->store_id), trim($request->type), trim($request->date_type));
		return $this->display($list);
	}


	/**
	 * 服务排名数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-20 17:54
	 */
	public function getOrderServiceData(Request $request)
	{
		$list = $this->statisticsService->getOrderServiceData(trim($request->store_id), trim($request->date_type), trim($request->is_amount), trim($request->is_limit));
		return $this->display($list);
	}


	/**
	 * 订单服务排名列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 * @author Happy dong
	 * @modifiedTime 2019-08-20 19:20
	 */
	public function getOrderServiceList(Request $request)
	{
		$list = $this->statisticsService->getOrderServiceList(trim($request->store_id), trim($request->service_id), trim($request->page_size), trim($request->date_type));
		$list = Utilities::dealPage($list);
		return $this->display($list);
	}


	/**
	 * 贡献数据
	 * @param Request $request
	 * @return mixed
	 * @author Happy dong
	 * @modifiedTime 2019-08-20 21:36
	 */
	public function getContributionAmountData(Request $request)
	{
		$list = $this->statisticsService->getContributionAmount(trim($request->store_id), trim($request->date_type), trim($request->is_barber), trim($request->is_limit));
		return $this->display($list);
	}

	/**
	 * 服务贡献列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getContributionServiceList(Request $request)
	{
		$list = $this->statisticsService->getContributionServiceList(trim($request->store_id), trim($request->service_id), trim($request->date_type));
		return $this->display($list);
	}


	/**
	 * 理发师贡献列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getContributionBarberList(Request $request)
	{
		$list = $this->statisticsService->getContributionBarberList(trim($request->store_id), trim($request->user_id), trim($request->page_size), trim($request->date_type));
		return $this->display((new getContributionListTransformer($list))->transformer());
	}


	/**
	 * 店长数据中心工资数据
	 * @param Request $request
	 * @return mixed
	 */
	public function getCenterUserSalaryData(Request $request)
	{
		$list = $this->statisticsService->getCenterUserSalaryData(trim($request->store_id));
		return $this->display($list);
	}


	/**
	 * 店长数据中心工资列表
	 * @param Request $request
	 * @return @mixegetContributionAmountDatad
	 */
	public function getCenterUserSalaryList(Request $request)
	{
		$list = $this->statisticsService->getCenterUserSalaryList(trim($request->store_id), trim($request->user_id), trim($request->month));
		return $this->display($list);
	}


	/**
	 * 店长端店铺经营数据
	 * @param Request $request
	 * @return mixed
	 */
	public function getStoreBusinessData(Request $request)
	{
		$date_type = $request->input('date_type', 0);
		$list = $this->statisticsService->getStoreBusinessData($date_type, $request->store_id);
		return $this->display($list);
	}


	/**
	 * 店长端店铺经营数据列表
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getStoreBusinessList(Request $request)
	{
		$date_type = $request->input('date_type', 0);
		$pageSize = $request->input('page_size', 10);
		switch ($request->type) {
			case 'jx':
				$type = [7, 9];
				break;
			case 'tr':
				$type = [8, 10];
				break;
			case 'qt':
				$type = [11];
				break;
			default:
				$type = $request->type;
		}
		$list = $this->statisticsService->getStoreBusinessList($date_type, $request->store_id, $type, $pageSize);
		return $this->display($list);
	}

	/**
	 * 店铺利润
	 * @param Request $request
	 * @return mixed
	 */
	public function getStoreProfit(Request $request)
	{
		$dateType = $request->input('date_type', 0);
		$data = $this->statisticsService->getStoreProfit($dateType, $request->store_id);
		return $this->display($data);

	}

	/**
	 * 支付方式店铺分析
	 * @param Request $request
	 * @return mixed
	 */
	public function getStorePayment(Request $request){
		$dateType = $request->input('date_type', 0);
		$data = $this->statisticsService->getStorePayment($dateType, $request->store_id);
		return $this->display($data);
	}


	public function getStorePaymentList(Request $request){
		$dateType = $request->input('date_type', 0);
		$pageSize = $request->input('page_size',10);
		$payment = $request->input('payment',1);
		$data = $this->statisticsService->getStorePaymentList($dateType, $request->store_id,$payment,$pageSize);
		return $this->display($data);
	}

	/**
	 * 员工业绩
	 * @param Request $request
	 * @return mixed
	 */
	public function getStoreAchievement(Request $request){
		$dateType = $request->input('date_type', 0);
		$data = $this->statisticsService->getStoreAchievement($dateType, $request->store_id);
		return $this->display($data);
	}
}
